import { ref, onMounted } from 'vue';
import { listDept } from '@/api/system/dept';
import {listClasses } from '@/api/core/classes';
import type { DeptVO } from '@/api/system/dept/types';

// 定义学校选项的接口
interface DeptOptionsType {
  deptId: number | string;
  deptName: string;
  children: DeptOptionsType[];
}

// 封装的公共方法
export function useSchoolList(): {
  deptOptions: DeptOptionsType[];
  fetchSchoolList: () => Promise<void>;
} {
  const deptOptions = ref<DeptOptionsType[]>([]);

  const fetchSchoolList = async (): Promise<void> => {
    try {
      const res = await listDept();
      const data = handleTree(res.data, 'deptId');
      if (data) {
        deptOptions.value = data;
      }
    } catch (error) {
      console.error('Failed to fetch school list:', error);
    }
  };

  onMounted(fetchSchoolList); // 可选：如果需要在组件挂载时自动加载数据

  return { deptOptions, fetchSchoolList };
}

// 树形结构处理函数
function handleTree<T extends { [key: string]: any }>(data: T[], idField: string): T[] {
  const tree = [] as T[];
  const nodeMap = new Map<string, T>();

  data.forEach(item => {
    nodeMap.set(item[idField], item);
    item.children = [];
  });

  data.forEach(item => {
    const parentId = item['parentId'];
    const parent = nodeMap.get(parentId);
    if (parent) {
      parent.children.push(item);
    } else {
      tree.push(item);
    }
  });

  return tree;
}

//重写学校下拉框--用租户数据
import { getTenantList } from '@/api/login';
interface tenantOptionsType {
  tenantId: number | string;
  companyName: string;
  domain: any;
}
   
export function useTenantlList(): {
  tenantOptions: tenantOptionsType[];
  tenantDisabled: Boolean;
  fetchTenantlList: (tenantId: ref<string | number>) => Promise<void>;
  
} {
  const tenantOptions = ref<tenantOptionsType[]>([]);
  const tenantDisabled = ref(true);
  const fetchTenantlList = async (tenantId: ref<string | number>): Promise<void> => {
    console.log('传参==',tenantId)
    try {
      const { data } = await getTenantList({areaCode:tenantId});
      tenantDisabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
      data.voList.forEach(e=>{
        e.tenantId = +e.tenantId;
      })
      console.log('租户列表======',data.voList);
      tenantOptions.value = data.voList;
    } catch (error) {
      console.error('Failed to fetch tenant list:', error);
    }
  };

  onMounted(fetchTenantlList); // 可选：如果需要在组件挂载时自动加载数据

  return { tenantOptions, fetchTenantlList,tenantDisabled };
}

//定义班级选项
interface classOptionsType {
  id: number | string;
  className: string;
}
// 封装的公共方法
export function useClassList(): {
  classOptions: classOptionsType[];
  fetchClasslList: (deptId: ref<string | number>) => Promise<void>;
} {
  const classOptions = ref<classOptionsType[]>([]);

  const fetchClasslList = async (deptId: ref<string | number>): Promise<void> => {
    try {
      const res = await listClasses({pageNum: 1,pageSize: 100,deptId:deptId});
      classOptions.value = res.rows;
    } catch (error) {
      console.error('Failed to fetch teacher list:', error);
    }
  };

  onMounted(fetchClasslList); // 可选：如果需要在组件挂载时自动加载数据

  return { classOptions, fetchClasslList };
}